package com.lijun.SpringBootJavaFX.controller.login;

import com.lijun.SpringBootJavaFX.controller.main.MainController;
import com.lijun.SpringBootJavaFX.utils.GetScreen;
import de.felixroske.jfxsupport.FXMLController;
import javafx.application.Platform;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.scene.control.Tooltip;
import javafx.stage.Stage;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

@FXMLController
public class LoginController {

    Logger logger = LoggerFactory.getLogger(LoginController.class);

    @FXML
    TextField userName;

    @FXML
    PasswordField password;

    @FXML
    Button submit;

    @Autowired
    MainController mainController;

    public void login(ActionEvent event){
        String defaultUserName = "test";
        String defaultPassword = "123";
        String userNameText = userName.getText();//获取当前输入的账号密码
        String passwordText = password.getText();
        if(StringUtils.isBlank(userNameText)){
            Tooltip tooltip = new Tooltip();
            tooltip.setText("请输入用户名");
            tooltip.show(userName,GetScreen.getScreenX(userName),GetScreen.getScreenY(userName)+25);
            tooltip.setAutoHide(true);
            return;
        }
        if(StringUtils.isBlank(passwordText)){
            Tooltip tooltip = new Tooltip();
            tooltip.setText("请输入密码");
            tooltip.show(password,GetScreen.getScreenX(password),GetScreen.getScreenY(password)+25);
            tooltip.setAutoHide(true);
            return;
        }
        if(defaultUserName.equals(userNameText)&&defaultPassword.equals(passwordText)){//判断是否正确
            System.out.println("登录成功!");
            Stage primaryStage=(Stage)submit.getScene().getWindow();//将submit(登录按钮)与Main类中的primaryStage(新窗口)绑定 并执行close()
            primaryStage.close();//打开新的窗口 所以要关闭当前的窗口
            //mStage.close();

            //打开主程序窗口
            mainController.openMainWindow();
        }else{
            Tooltip tooltip = new Tooltip();
            tooltip.setText("用户名或密码错误");
            tooltip.show(submit,GetScreen.getScreenX(submit),GetScreen.getScreenY(submit)+25);
            tooltip.setAutoHide(true);
        }
    }

    public void closeWin(){
        logger.info("取消登录，退出系统!");
        Platform.exit();
    }
}